/**
 * Copyright 2013 ABSir's Studio
 * 
 * All right reserved
 *
 * Create on 2013-6-18 上午9:40:39
 */
package com.absir.bean.inject;

import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;

import com.absir.bean.basis.BeanDefine;
import com.absir.bean.core.BeanFactoryImpl;

/**
 * @author absir
 * 
 */
public class InjectSoftObserver {

	/** softReference */
	SoftReference<Object> softReference;

	/** injectInvokers */
	List<InjectInvoker> injectInvokers = new ArrayList<InjectInvoker>();

	/**
	 * @param beanObject
	 */
	public InjectSoftObserver(Object beanObject) {
		this.softReference = new SoftReference<Object>(beanObject);
	}

	/**
	 * @param injectInvoker
	 */
	public void add(InjectInvoker injectInvoker) {
		injectInvokers.add(injectInvoker);
	}

	/**
	 * @return
	 */
	public boolean isEmpty() {
		return injectInvokers.size() <= 0;
	}

	/**
	 * @param beanFactory
	 * @param beanDefine
	 */
	public boolean changed(BeanFactoryImpl beanFactory, BeanDefine beanDefine, boolean register) {
		Object beanObject = softReference.get();
		if (beanObject == null) {
			return false;
		}

		for (InjectInvoker injectInvoker : injectInvokers) {
			if (injectInvoker.support(beanDefine.getBeanType())) {
				injectInvoker.invoke(beanFactory, beanObject);
			}
		}

		return true;
	}
}
